<?php

declare(ENCODING = 'UTF-8');

/*                                                                        *
 * This script is part of the TYPO3 project - inspiring people to share!  *
 *                                                                        *
 * TYPO3 is free software; you can redistribute it and/or modify it under *
 * the terms of the GNU General Public License version 2 as published by  *
 * the Free Software Foundation.                                          *
 *                                                                        *
 * This script is distributed in the hope that it will be useful, but     *
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *	
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
 * Public License for more details.                                       *
 *                                                                        */	

/**
 * @package auxo
 * @subpackage Web 
 * @version $Id$
 */

/**	
 * The Tidy HTML Filter
 *	
 * @package auxo
 * @subpackage Web
 * @version $Id$	
 * @copyright Copyright belongs to the respective authors
 * @author AHN
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
 */
class  tx_auxo_TidyFilter implements tx_auxo_FilterInterface {
	/**
	 * Tidy options as array 
	 *
	 * @var array
	 */
	private $tidyOptions = array();
	
	/**
	 * flag which is true if tidy is available
	 *
	 * @var boolean
	 */
	private $active = false;
	
	/**
	 * Initialize this filter with an configuration instances
	 * 
	 * Tidy options are extracted from configuration and extension existence 
	 * is checked.
	 *
	 * @param tx_auxo_Configuraiton $configuraiton
	 */
	public function initialize(tx_auxo_Configuraiton $configuraiton) {
		$this->active = extension_loaded('tidy');
		$this->tidyOptions = $this->configuration->get('tidy.');
	}
	
	/**
	 * Filters the content of the repsonse using extension tidy 
	 * with a set of configured options
	 *
	 * @param tx_auxo_Request $request a request
	 * @param tx_auxo_Response $response a response
	 * @param tx_auxo_FilterChain $chain a filter Chain
	 */
	public function	filter(tx_auxo_Request $request, tx_auxo_Response $response, tx_auxo_FilterChain $chain) {
		$chain->invoke($request, $response);
		
		if ($this->active) {
			$response->setContent(tidy_repair_string($response->getContent(), $this->tidyOptions, 'utf8'));
		}
	}
}
?>